/**
 * 
 */
package com.zito.manager;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.downlove.common.RequestContext;
import com.downlove.common.StorageService;
import com.downlove.common.Storage.ImageInfo;
import com.downlove.database.SequenceManager;
import com.zito.ErrMsgException;
import com.zito.bean.Weblink;
import com.zito.common.Paginator;

/**
 * @author YunHai-Home
 * 
 */
public class WeblinkManager {

	Weblink link = new Weblink();
	private Paginator paginator;
	int logoSize = 150 * 1024;

	@SuppressWarnings("unchecked")
	public List<Weblink> textList(int pagesize) {
		RequestContext rc = RequestContext.get();
		String filter = "islogo=0";
		int total = link.TotalCount(filter);

		this.paginator = new Paginator(rc, total, pagesize);
		// 设置当前页数和总页数
		int totalpages = this.paginator.getTotalPages();
		int curpage = this.paginator.getCurrentPage();
		if (totalpages == 0) {
			curpage = 1;
			totalpages = 1;
		}
		List<Long> tmpList = link.idListFilter(filter, curpage, pagesize);
		return link.LoadList(tmpList);
	}

	@SuppressWarnings("unchecked")
	public List<Weblink> textList(String types) {
		String filter = "types='" + types + "'";
		int total = link.TotalCount(filter);
		List<Long> tmpList = link.idListFilter(filter, 1, total);
		return link.LoadList(tmpList);
	}

	@SuppressWarnings("unchecked")
	public List<Weblink> logoList(int pagesize) {
		RequestContext rc = RequestContext.get();
		String filter = "islogo=1";
		int total = link.TotalCount(filter);

		this.paginator = new Paginator(rc, total, pagesize);
		// 设置当前页数和总页数
		int totalpages = this.paginator.getTotalPages();
		int curpage = this.paginator.getCurrentPage();
		if (totalpages == 0) {
			curpage = 1;
			totalpages = 1;
		}
		List<Long> tmpList = link.idListFilter(filter, curpage, pagesize);
		return link.LoadList(tmpList);
	}

	@SuppressWarnings("unchecked")
	public List<Weblink> showLogoList(int pagesize) {
		String filter = "islogo=1";
		List<Long> tmpList = link.idListFilter(filter, 1, pagesize);
		return link.LoadList(tmpList);
	}

	public String getPaginator() {
		return this.paginator.getCssPageBlock();
	}

	public Weblink addLink() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		link = null;
		link = rc.form(Weblink.class);
		String error = link.doValidator();
		if (!StringUtils.isBlank(error))
			throw new ErrMsgException(error);
		link.setId(SequenceManager.nextID(Weblink.class.getSimpleName()));
		link.setTypes("");
		link.Save();
		return link;
	}

	public Weblink addTextLink() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		link = null;
		link = rc.form(Weblink.class);
		String error = link.doValidator();
		if (!StringUtils.isBlank(error))
			throw new ErrMsgException(error);
		link.setIslogo(0);
		link.setTypes("");
		link.setId(SequenceManager.nextID(Weblink.class.getSimpleName()));
		link.Save();
		return link;
	}

	public boolean deleteTextLink() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		link.setId(rc.param("id", -1L));
		if (link.getId() == -1)
			return false;
		link = link.Get(link.getId());
		boolean flag = link.Delete();
		if (flag)
			if (link.getIslogo() == 1)
				FileUtils.deleteQuietly(new File(RequestContext.root()+link.getLogo()));
		return true;
	}

	public boolean updateTextLink() throws ErrMsgException {
		RequestContext rc = RequestContext.get();
		link = null;
		link = rc.form(Weblink.class);
		if (link.getId() == -1)
			return false;
		String error = link.doValidator();
		if (!StringUtils.isBlank(error))
			throw new ErrMsgException(error);
		if (link.getIslogo() == 1) {
			Weblink _oldLink = new Weblink();
			_oldLink = _oldLink.Get(link.getId());
			if (!_oldLink.getLogo().equals(link.getLogo())) {
				FileUtils.deleteQuietly(new File(RequestContext.root()+_oldLink.getLogo()));
			}
		}
		return link.update();
	}

	public ImageInfo uploadLOGO() throws ErrMsgException, IOException {
		RequestContext rc = RequestContext.get();
		Enumeration<String> files = rc.getFileNames();
		ImageInfo info = null;
		while (files.hasMoreElements()) {
			String filename = files.nextElement();
			File imgFile = rc.image(filename);
			if (imgFile.length() > logoSize) {
				throw new ErrMsgException("您上传的- " + imgFile.getName()
						+ " -图片过大，系统仅允许上传：" + logoSize / 1024 + "K 大小的文件");
			}
			info = StorageService.IMAGES.saveImage(imgFile).getImageInfo();
		}
		return info;
	}

}
